package yl.hs.bmipfull.service.laboratorydata.imple;


import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratorydata.ILabCertificateDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabCertificateRmsDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabCertificateSpecimensDao;
import yl.hs.bmipfull.pojo.laboratorydata.*;
import yl.hs.bmipfull.service.laboratorydata.ILabCertificateRmsService;
import yl.hs.bmipfull.service.laboratorydata.ILabCertificateService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴高丰
 * @apiNote 合格证信息服务层接口实现
 * @since 2022年07月20日 15时23分53秒 <br/>
 */
@Service
public class LabCertificateServiceImpl extends LaboratorydataBaseService implements ILabCertificateService {

    @Autowired
    ILabCertificateDao dao;
    @Autowired
    ILabCertificateRmsDao rmsdao;
    @Autowired
    ILabCertificateSpecimensDao certificateSpecimensDao;
    @Override
    public List<LabCertificateEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabCertificateEntity.class);
        pageQuery.getQuery().put("sortKey", String.join(",", pageQuery.getSortSides()));
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), LabCertificateEntity.class);
        return dao.queryList(pageQuery.getQuery(), filters);
    }

    @Override
    public PageResult<LabCertificateEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabCertificateEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabCertificateEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),LabCertificateEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }


    @Override
    public LabCertificateEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveLabCertificate(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        LabCertificateEntity entity = null;
        LabCertificateRmsEntity rmsEntity = null;
        LabCertificateSpecimensEntity specimensEntity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new LabCertificateEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(LabCertificateEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入合格证信息信息", Tl.toJson(entity), LabCertificateEntity.class);
            }
            if (model.get("subData")!=null && model.get("subData")!="") {
                var subData = (List<Map<String, Object>>) model.get("subData");
                for (var item : subData) {
                    rmsEntity = new LabCertificateRmsEntity();
                    Tl.getChangeLog(rmsEntity, item);
                    rmsEntity.setPYFieldValue();
                    rmsEntity.setCertificateSN(entity.getSn());
                    result = rmsdao.insertNew(rmsEntity);
                    if (result > 0) {
                        writeLog(ActionType.CREATE, entity.getSn(), "插入合格证原材信息信息", Tl.toJson(rmsEntity), LabCertificateRmsEntity.class);
                    }
                }
            }
            if (model.get("formItem") != null && model.get("formItem")!="") {
                var formItem = (List<Map<String, Object>>) model.get("formItem");
                for (var item : formItem) {
                    specimensEntity = new LabCertificateSpecimensEntity();
                    Tl.getChangeLog(specimensEntity, item);
                    specimensEntity.setPYFieldValue();
                    specimensEntity.setCertificateSN(entity.getSn());
                    result = certificateSpecimensDao.insertNew(specimensEntity);
                    if (result > 0) {
                        writeLog(ActionType.CREATE, entity.getSn(), "插入合格证试件信息", Tl.toJson(specimensEntity), LabCertificateSpecimensEntity.class);
                    }
                }
            }
        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新合格证信息信息", Tl.toJson(entity), LabCertificateEntity.class);
            }
            rmsdao.deleteByCertifiSN(entity.getSn());
            if (model.get("subData")!=null && model.get("subData")!="") {
                var subData = (List<Map<String, Object>>) model.get("subData");
                for (var item : subData) {
                    rmsEntity = new LabCertificateRmsEntity();
                    Tl.getChangeLog(rmsEntity, item);
                    rmsEntity.setPYFieldValue();
                    rmsEntity.setCertificateSN(entity.getSn());
                    result = rmsdao.insertNew(rmsEntity);
                    if (result > 0) {
                        writeLog(ActionType.CREATE, entity.getSn(), "插入合格证原材信息信息", Tl.toJson(rmsEntity), LabCertificateRmsEntity.class);
                    }
                }
            }
            certificateSpecimensDao.deleteByCertifiSN(entity.getSn());
            if (model.get("formItem") != null && model.get("formItem")!="") {
                var formItem = (List<Map<String, Object>>) model.get("formItem");
                for (var item : formItem) {
                    specimensEntity = new LabCertificateSpecimensEntity();
                    Tl.getChangeLog(specimensEntity, item);
                    specimensEntity.setPYFieldValue();
                    specimensEntity.setCertificateSN(entity.getSn());
                    result = certificateSpecimensDao.insertNew(specimensEntity);
                    if (result > 0) {
                        writeLog(ActionType.CREATE, entity.getSn(), "插入合格证试件信息", Tl.toJson(specimensEntity), LabCertificateSpecimensEntity.class);
                    }
                }
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, LabCertificateEntity.class);
        }
        return result;
    }

    @Override
    public Object querySubTable(String sn) {
        Map<String,Object> map = new HashMap<String,Object>();
        var caterm = rmsdao.queryByCertificateSN(sn);
        val labCertificateSpecimensEntities = certificateSpecimensDao.queryByCertificateSN(sn);
        map.put("subData",caterm);
        map.put("formItem",labCertificateSpecimensEntities);
        return map;
    }
}